package com.tsxy.mall.service.impl;

import com.tsxy.mall.entity.*;
import com.tsxy.mall.mapper.BookMapper;
import com.tsxy.mall.mapper.CartMapper;
import com.tsxy.mall.mapper.OrderDetailsMapper;
import com.tsxy.mall.mapper.OrderMapper;
import com.tsxy.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private BookMapper bookMapper;

    @Override
    @Transactional
    public Order add(Integer[] cartIds, Integer userId) {

        List<Cart> carts = new ArrayList<>();
        for (int cartId : cartIds) {
            Cart cart = cartMapper.getById(cartId);
            carts.add(cart);
        }
        // 1. 添加订单信息
        Order order = new Order();
        order.setUserId(userId);
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String user = UUID.randomUUID().toString().replace("-","").toUpperCase();
        String orderNum = time+user;
        order.setId(orderNum);
        int result = orderMapper.insert(order);
        for (int i =0; i < carts.size(); i++){
            OrderDetails orderDetail = new OrderDetails();
            if ("0".equals(carts.get(i).getBookType())){
                BigDecimal s = carts.get(i).getBook().getPrice().divide(new BigDecimal(10), 2).multiply(new BigDecimal(carts.get(i).getBook().getDisCount()));
                orderDetail.setResultPrice(s);
                orderDetail.setPrice(carts.get(i).getBook().getPrice() );
            }else {
                BigDecimal s = carts.get(i).getUsedBook().getPrice().divide(new BigDecimal(10), 2).multiply(new BigDecimal(carts.get(i).getUsedBook().getDisCount()));
                orderDetail.setResultPrice(s);
                orderDetail.setPrice(carts.get(i).getUsedBook().getPrice() );
            }
            orderDetail.setOrderId(order.getId());
            orderDetail.setBookId(carts.get(i).getBookId());
            orderDetail.setCount(carts.get(i).getCount());
            orderDetail.setBookType(carts.get(i).getBookType());
            //保存到数据库
            result = orderDetailsMapper.insert(orderDetail);
        }
        for (int i = 0 ; i < carts.size(); i++){
            // result = cartMapper.deleteByPrimaryKey(carts.get(i).getId());

            // 6-扣除商品的库存
            if (carts.get(i).getBookType() == "0"){
                Book book = carts.get(i).getBook();
                book.setCount(book.getCount() - carts.get(i).getCount());
                result = bookMapper.updateCount(book);
            }else {
                UsedBook usedBook = carts.get(i).getUsedBook();
            }

        }

        return orderMapper.selectByPrimaryKey(order.getId());
    }

    @Override
    public Integer updateStatus(Order order) {
        return orderMapper.updateStatus(order);
    }

    @Override
    public List getAll() {

        return orderMapper.getAll();
    }
}
